package com.bingxue.edu.management.education.controller;


import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.education.model.entity.TimeTemplate;
import com.bingxue.edu.management.education.model.req.TimeTemplateReq;
import com.bingxue.edu.management.education.model.resp.TimeTemplateResp;
import com.bingxue.edu.management.education.service.TimeTemplateService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.TimeTemplateTableDef.TIME_TEMPLATE;

@Slf4j
@Tag(name = "上课时间段接口")
@RestController
@RequestMapping("/education/time-template")
@RequiredArgsConstructor
public class TimeTemplateController {
    private final TimeTemplateService timeTemplateService;

    @Operation(summary ="查询列表", description = "查询列表")
    @ApiResource("system:biz-info:manage")
    @GetMapping("/list")
    public Response<List<TimeTemplateResp>> list(){
        QueryWrapper queryWrapper = QueryWrapper.create().orderBy(TIME_TEMPLATE.START_TIME, true);
        List<TimeTemplateResp> result = timeTemplateService.listAs(queryWrapper, TimeTemplateResp.class);
        return Response.success(result);
    }

    @Operation(summary ="查询详情", description = "查询详情")
    @ApiResource("system:biz-info:manage")
    @GetMapping("/{id}")
    public Response<TimeTemplateResp> detail(@PathVariable String id){
        TimeTemplateResp detail = timeTemplateService.getMapper().selectOneByIdAs(id, TimeTemplateResp.class);
        return Response.success(detail);
    }

    @Operation(summary = "创建数据",description = "创建数据")
    @ApiResource("system:biz-info:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody TimeTemplateReq req){
        TimeTemplate timeTemplate = timeTemplateService.copy(req);
        timeTemplateService.save(timeTemplate);
        return Response.success();

    }

    @Operation(summary = "更新数据",description = "更新数据")
    @ApiResource("system:biz-info:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody TimeTemplateReq req, @PathVariable String id){
        TimeTemplate updateTimeTemplate = timeTemplateService.copyById(req, id);
        timeTemplateService.updateById(updateTimeTemplate);
        return Response.success();
    }


    @Operation(summary = "删除数据",description = "删除数据")
    @ApiResource("system:biz-info:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids){
        timeTemplateService.remove(ids);
        return Response.success();
    }
}
